import { URL, ajax, Toast, getUser } from '../../utils/index.js'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        oaCode:'',
        pws:{
            password:'',
            newPassword:'',
            newPasswordTow:'',
        },
        errorPassword: '',
        errorNewPassword: '',
        errorNewPasswordTow: '',
    },
    setUser(data) {
        this.setData({
            oaCode: data.oaCode
        })
    },
    input(e) {
        let userkey = 'pws.' + e.currentTarget.dataset.inputname
        this.setData({
            [userkey]: e.detail
        })
    },
    btnsubmit(){
        let {password,newPassword,newPasswordTow} = this.data.pws,oaCode = this.data.oaCode,errorPassword,errorNewPassword,errorNewPasswordTow;
        !password?errorPassword="请输入原密码":errorPassword='',!newPassword?errorNewPassword="请输入新密码":errorNewPassword='',!newPasswordTow?errorNewPasswordTow="请输入确认密码":errorNewPasswordTow='',
        newPassword && newPasswordTow? newPassword===newPasswordTow?errorNewPasswordTow="":errorNewPasswordTow='两次填写的密码不一致':'',
        this.setData({errorPassword,errorNewPassword,errorNewPasswordTow});
        if(!password||!newPassword||!newPasswordTow || errorNewPasswordTow!='') return;
        this.upDatePss({ password,newPassword, oaCode })
    },
    upDatePss ({ password,newPassword, oaCode }) {
        return new Promise((resolve, reject) => {
            ajax.loading(), ajax.post('/applet/user/updatePassword', { password,newPassword, oaCode }).then(
                res => res.data.code == 1 ? (
                Toast({
                    type: 'success',
                    forbidClick: true,
                    message: res.data.msg,
                    duration:1000,
                    onClose: () => (wx.navigateBack({delta:1}))
                  })
                ) : (ajax.toast.fail(res.data.msg), reject(res))).catch(e => (ajax.toast.fail('接口错误updateUserInfo'), reject(e), console.log(e)));
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        getUser().then(res => this.setUser(res.data.data))
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})